#include "MainPower.h"
#include "TinyXML/tinyxml.h"
#include "ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

MainPower::MainPower()
{

}

void MainPower::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QList<MainPowerSlot> _MainPowerSlotList;
    QList<MainPoweritem> _MainPoweritemList;
    Analysis(filePath, _MainPowerSlotList, _MainPoweritemList);

    //添加第一个表
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    MainPowerSlot _MainPowerSlottemp;
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_MainPowerSlottemp.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_MainPowerSlottemp.toFieldTypes());

    for(int j = 0;j<_MainPowerSlotList.size();j++)
    {
        MainPowerSlot _MainPowerSlot = _MainPowerSlotList.at(j);
        int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
        _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(QString("%1").arg(_MainPowerSlot.ac_id)));
        _TableWidgetForm->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,_MainPowerSlot.id);
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_MainPowerSlot.out_id)));
    }

    //添加第二个表
    TableWidgetForm* _TableWidgetForm2 = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm2);
    _TableWidgetForm2->setGroupBoxTitle(QString("%1_1").arg(fileName.left(fileName.size()-4)));

    //获取表头
    MainPoweritem _MainPoweritemtemp;
    _TableWidgetForm2->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_MainPoweritemtemp.toFieldNames().split(',')));

    for(int j = 0;j<_MainPoweritemList.size();j++)
    {
        MainPoweritem _MainPoweritem = _MainPoweritemList.at(j);
        int rowCount = _TableWidgetForm2->getTableWidget()->rowCount();
        _TableWidgetForm2->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(QString("%1").arg(_MainPoweritem.name)));
        _TableWidgetForm2->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,_MainPoweritem.id);
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_MainPoweritem.soft_id)));
    }
    m_pTableWidgetFormMap.insert(0,  QList<TableWidgetForm*>() << _TableWidgetForm << _TableWidgetForm2);
}

bool MainPower::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.count() == 1)
    {
        QList<MainPowerSlot> _MainPowerSlotList;
        QList<MainPoweritem> _MainPoweritemList;

        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            for (int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                MainPowerSlot _MainPowerSlot;
                _MainPowerSlot.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _MainPowerSlot.ac_id = _TableWidgetForm->getTableWidget()->item(k,0)->text().toInt();
                _MainPowerSlot.out_id = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                _MainPowerSlotList.append(_MainPowerSlot);
            }
        }

        TableWidgetForm* _TableWidgetForm2 = m_pTableWidgetFormMap.value(0).at(1);
        if(_TableWidgetForm2)
        {
            for (int k = 0;k<_TableWidgetForm2->getTableWidget()->rowCount();k++)
            {
                MainPoweritem _MainPoweritem;
                _MainPoweritem.id = _TableWidgetForm2->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _MainPoweritem.name = _TableWidgetForm2->getTableWidget()->item(k,0)->text();
                _MainPoweritem.soft_id = _TableWidgetForm2->getTableWidget()->item(k,1)->text().toInt();
                _MainPoweritemList.append(_MainPoweritem);
            }
        }
        SaveXml(filePath,_MainPowerSlotList,_MainPoweritemList);
        return true;
    }
    return false;
}

void MainPower::Analysis(const QString &path, QList<MainPowerSlot> &_MainPowerSlotList, QList<MainPoweritem> &_MainPoweritemList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("MainPower");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        TiXmlElement* SlotsElement = rootElement->FirstChildElement("Slots");
        if(SlotsElement != nullptr)
        {
            for (TiXmlElement* SlotElement = SlotsElement->FirstChildElement("Slot");
                 SlotElement != nullptr; SlotElement = SlotElement->NextSiblingElement("Slot")) {
                if(SlotElement != nullptr)
                {
                    MainPowerSlot _MainPowerSlot;
                    SlotElement->QueryIntAttribute("id", &_MainPowerSlot.id);
                    SlotElement->QueryIntAttribute("ac_id", &_MainPowerSlot.ac_id);
                    SlotElement->QueryIntAttribute("out_id", &_MainPowerSlot.out_id);
                    _MainPowerSlotList.append(_MainPowerSlot);
                }
            }
        }

        TiXmlElement* OthersElement = rootElement->FirstChildElement("Others");
        if(OthersElement != nullptr)
        {
            for (TiXmlElement* itemElement = OthersElement->FirstChildElement("item");
                 itemElement != nullptr; itemElement = itemElement->NextSiblingElement("item")) {
                if(itemElement != nullptr)
                {
                    MainPoweritem _MainPoweritem;
                    itemElement->QueryIntAttribute("id", &_MainPoweritem.id);
                    _MainPoweritem.name = itemElement->Attribute("name");
                    itemElement->QueryIntAttribute("soft_id", &_MainPoweritem.soft_id);
                    _MainPoweritemList.append(_MainPoweritem);
                }
            }
        }
    }
    delete xmlDocument;
}

bool MainPower::SaveXml(const QString &path, const QList<MainPowerSlot> &_MainPowerSlotList, const QList<MainPoweritem> &_MainPoweritemList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("MainPower");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        TiXmlElement* SlotsElement = rootElement->FirstChildElement("Slots");
        if(SlotsElement != nullptr)
        {
            for (TiXmlElement* SlotElement = SlotsElement->FirstChildElement("Slot");
                 SlotElement != nullptr; SlotElement = SlotElement->NextSiblingElement("Slot")) {
                if(SlotElement != nullptr)
                {
                    SlotsElement->RemoveChild(SlotElement);
                }
            }
            for (auto _MainPowerSlot : _MainPowerSlotList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("Slot");
                ChildxmlElement->SetAttribute("id", _MainPowerSlot.id);
                ChildxmlElement->SetAttribute("ac_id", _MainPowerSlot.ac_id);
                ChildxmlElement->SetAttribute("out_id", _MainPowerSlot.out_id);
                SlotsElement->LinkEndChild(ChildxmlElement);
            }
        }

        TiXmlElement* OthersElement = rootElement->FirstChildElement("Others");
        if(OthersElement != nullptr)
        {
            for (TiXmlElement* itemElement = OthersElement->FirstChildElement("item");
                 itemElement != nullptr; itemElement = itemElement->NextSiblingElement("item")) {
                if(itemElement != nullptr)
                {
                    OthersElement->RemoveChild(itemElement);
                }
            }
            for (auto _MainPoweritem : _MainPoweritemList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("item");
                ChildxmlElement->SetAttribute("id", _MainPoweritem.id);
                ChildxmlElement->SetAttribute("name", _MainPoweritem.name.toStdString().c_str());
                ChildxmlElement->SetAttribute("soft_id", _MainPoweritem.soft_id);
                OthersElement->LinkEndChild(ChildxmlElement);
            }
        }
    }
    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
